1 package org.apache.lucene.analysis.ja.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.io.BufferedOutputStream;
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25
26 import org.apache.lucene.analysis.ja.dict.ConnectionCosts;
27
28 import org.apache.lucene.codecs.CodecUtil;
29 import org.apache.lucene.store.DataOutput;
30 import org.apache.lucene.store.OutputStreamDataOutput;
31 import org.apache.lucene.util.BitUtil;
32
33 public final class ConnectionCostsWriter {
34
35 private final short[][] costs;
36 private final int forwardSize;
37 private final int backwardSize;
38
39
40
41 public ConnectionCostsWriter(int forwardSize, int backwardSize) {
42 this.forwardSize = forwardSize;
43 this.backwardSize = backwardSize;
44 this.costs = new short[backwardSize][forwardSize];
45 }
46
47 public void add(int forwardId, int backwardId, int cost) {
48 this.costs[backwardId][forwardId] = (short)cost;
49 }
50
51 public void write(String baseDir) throws IOException {
52 String filename = baseDir + File.separator +
53 ConnectionCosts.class.getName().replace('.', File.separatorChar) + ConnectionCosts.FILENAME_SUFFIX;
54 new File(filename).getParentFile().mkdirs();
55 OutputStream os = new FileOutputStream(filename);
56 try {
57 os = new BufferedOutputStream(os);
58 final DataOutput out = new OutputStreamDataOutput(os);
59 CodecUtil.writeHeader(out, ConnectionCosts.HEADER, ConnectionCosts.VERSION);
60 out.writeVInt(forwardSize);
61 out.writeVInt(backwardSize);
62 int last = 0;
63 assert costs.length == backwardSize;
64 for (short[] a : costs) {
65 assert a.length == forwardSize;
66 for (int i = 0; i < a.length; i++) {
67 int delta = (int)a[i] - last;
68 out.writeZInt(delta);
69 last = a[i];
70 }
71 }
72 } finally {
73 os.close();
74 }
75 }
76
77 }